new Vue({
    el: '#app',
    data() {
        return {
            formData: {
                Land_SerialNumber: '',
                Operation_Photo: '',
                Crop_SerialNumber: '',
                Planting_People_SerialNumber: '',
                Operation_Type: '',
                Operation_Date: '',
                Growth_Stage: '',
                Material_Name: '',
                Material_Dosage: '',
                Dosage_Unit: '',
                Enterprise_Info_SerialNumber: getCookie('enterprise'),
                Open_ID: getCookie('openId'),
                Creater_SerialNumber: getCookie('user')
            },
            land_info: [],
            crop: [],
            planting_people: [],
            growth_stage: [],
            Operation_Type: [],
            loading: false,
        }
    },
    beforeCreate() {
        function setRem() {
            const designWidth = 750; // 设计稿宽度（rpx）
            const maxWidth = 540; // 可选：限制最大宽度（如手机端常见值）
            const screenWidth = Math.min(window.innerWidth, maxWidth);
            const scale = screenWidth / designWidth;
            document.documentElement.style.fontSize = scale + 'px';
        }
        setRem();//1rem = 0.5px 对标小程序在 375px 宽的屏幕上，1rpx = 0.5px（因为 750rpx / 375px = 2）
    },
    mounted() {
        this.init();
    },
    methods: {
        async init() {
            try {
                const [landRes, cropRes, peopleRes] = await Promise.all([
                    generalApi.generalQueryByTableName({
                        Data: 'land_info',
                        Condition: `Row_Status='0'`
                    }),
                    generalApi.generalQueryByTableName({
                        Data: 'crop',
                        Condition: `Row_Status='0'`
                    }),
                    generalApi.generalQueryByTableName({
                        Data: 'planting_people',
                        Condition: `Row_Status='0'`
                    })
                ]);
                this.land_info = landRes;
                this.crop = cropRes;
                this.planting_people = peopleRes;
            } catch (error) {
                this.$showToast({ title: '系统错误，请联系开发者' });
            }
            $.ajax({
                url: absolutepath() + 'admin/data/Operation_Type.json',
                type: 'GET',
                success: (data) => {
                    this.Operation_Type = data;
                },
            });
            $.ajax({
                url: absolutepath() + 'admin/data/growth_stage.json',
                type: 'GET',
                success: (data) => {
                    this.growth_stage = data;
                },
            });
        },
        handleDate(val) {
            if (val == null) {
                this.formData.Operation_Date = '';
            }
        },
        async handleSubmit() {
            if (this.loading) return;
            const rules = [
                { key: 'Land_SerialNumber', message: '请选择地块编号' },
                { key: 'Operation_Photo', message: '请上传操作图片' },
                { key: 'Crop_SerialNumber', message: '请选择作物' },
                { key: 'Planting_People_SerialNumber', message: '请选择操作人' }
            ];
            for (const r of rules) {
                if (!this.formData[r.key]) {
                    this.$showToast({ title: r.message });
                    return;
                }
            }
            this.loading = true;
            const lJSONObject_Add = { farm_operation_record: [deepClone(this.formData)] };
            let lJSONObject_DataSet = { Add: lJSONObject_Add };
            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
            lJSONObject_DataSet['Add']['base_operation_log'] = [{
                Operation_Type: '1',
                Operation_Path: getRelativePath(),
                Operation_Description: '新增农事操作记录',
                Operation_Main,
                Operation_Interface: 'generalSaveByRequestBody',
                Operation_IP: getIpAddress()
            }];
            try {
                const res = await generalApi.generalSaveByRequestBody({ Data: lJSONObject_DataSet });
                if (res.Status == 'success') {
                    this.$showToast({ title: '提交成功' });
                    window.location.href = '../../pages/index/farm_operation_record.html';
                } else {
                    this.$showToast({ title: '提交失败，请重试！' });
                }
            } catch (error) {
                this.$showToast({ title: '系统错误，请联系开发者' });
            }
            this.loading = false;
        },
        handleBack() {
            window.location.href = '../../pages/index/farm_operation_record.html';
        }
    }
})
